<?php

class TingController extends Controller
{
	public $defaultAction ='ting';

	/**
	 * 随机听一个音频
	 */
	public function actionTing() {
		$criteria = new CDbCriteria();
		$criteria -> order ="RAND()";
		$audio = Audios::model() -> find($criteria);
	
		$this -> redirect('/shuo/'.$audio->audio_saltid);
		Yii::app() -> end();
	}
	
	public function actionShuo($id){
		
		$saltid = $id;
		$audio_model = Audios::model() -> findByAttributes(array('audio_saltid'=>$saltid));
		
		$this->pageTitle = $audio_model -> audio_name;
		
		if(!$audio_model){
			throw new CHttpException(404,'The requested page does not exist.');
		}
		
		$like_model = AudioLikes::model ();
		$uid = Yii::app ()->user->id;
		
		if ($uid) {
			$like_model = AudioLikes::model ()->findByAttributes ( array ('like_from' => $uid, 'audio_id' => $audio_model->audio_id ) );
		}
		
		if ($audio_model) {
		
			$audio_model->setHistory ( $audio_model );
				
			$playList = array(
					'title'=>$audio_model -> audio_name,
					'artist'=>$audio_model-> audio_from,
					'album'=>$audio_model-> audio_from,
					'cover'=>$audio_model->accessCover,
					'mp3'=>$audio_model->accessAudio,
					'ogg'=>$audio_model->accessAudio);
				
			$mp3file = '['.CJSON::encode($playList).']';
				
			$this->render ( 'view', array ('audio_model' => $audio_model, 'like_model' => $like_model ,'mp3file'=>$mp3file) );
		} else {
			$this->render ( 'notfound' );
		}
		
	}

	// Uncomment the following methods and override them if needed
	/*
	public function filters()
	{
		// return the filter configuration for this controller, e.g.:
		return array(
			'inlineFilterName',
			array(
				'class'=>'path.to.FilterClass',
				'propertyName'=>'propertyValue',
			),
		);
	}

	public function actions()
	{
		// return external action classes, e.g.:
		return array(
			'action1'=>'path.to.ActionClass',
			'action2'=>array(
				'class'=>'path.to.AnotherActionClass',
				'propertyName'=>'propertyValue',
			),
		);
	}
	*/
}